package com.bjpowernode.p2padmin.user.service.impl;

import com.bjpowernode.p2padmin.user.mapper.PermissionInfoMapper;
import com.bjpowernode.p2padmin.user.mapper.UserInfoMapper;
import com.bjpowernode.p2padmin.user.model.PermissionInfo;
import com.bjpowernode.p2padmin.user.model.UserInfo;
import com.bjpowernode.p2padmin.user.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName:UserServiceImpl
 * Package:com.bjpowernode.p2padmin.user.service.impl
 * Description:TODO描述
 *
 * @date:2020/4/6 11:28
 * @author:CC
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private PermissionInfoMapper permissionInfoMapper;

    @Override
    public UserInfo login(String username, String password) {

        UserInfo user =userInfoMapper.selectUserByusername(username);

        if (user==null){
            return null;
        }

        if (!user.getPassword().equals(password)){
            return null;
        }
        user.setLastlogintime(new Date());
        user.setLogincount(user.getLogincount()==null?1:user.getLogincount()+1);
        userInfoMapper.updateLoginCount(user);

        //根据当前用户获取所有的菜单权限
        //参数1:用户id
        //参数2:权限的父级id
        List<PermissionInfo> pis = permissionInfoMapper.selectPermissionMenuByUserId(user.getId(), 1);

        user.setMenuList(pis);

        List<String> urlList = permissionInfoMapper.selectPermissionUrlByUserId(user.getId());
        Map<String,String> urlMap=new HashMap<>();

        for (String url : urlList) {//将所有的权限url 存入map集合
            //使用权限url作为key,使用任意非空的作为value
            urlMap.put(url,"1");
        }
        user.setUrlMap(urlMap);

        return user;

    }


}
